﻿// Gateway.Api 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
// 
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
// 
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

using Aurora.AI.Abstractions.Chats.Dtos;
using Aurora.AI.Abstractions.ObjectModels.RequestModels;
using Aurora.AI.Abstractions.ObjectModels.RequestModels.VideoRequestModel;
using Aurora.AI.Gateway;
using Gateway.UI.Api.AiServices;
using Gateway.UI.Api.AiServices.Gateway;
using Volo.Abp;
using Volo.Abp.Modularity;
using Aurora.AI.Wiki;

namespace Gateway.UI.Api.AbpModules;

/// <summary>
/// ai 路由节点 abp module
/// </summary>
[DependsOn()]
public class AiEndpointModule : AbpModule
{
    /// <summary>
    /// configure service
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    public override Task ConfigureServicesAsync(ServiceConfigurationContext context)
    {
        context.Services.AddWikiAiService(context.Services.GetConfiguration());
        context.Services.AddGatewayAiService(context.Services.GetConfiguration());
        context.Services.AddScoped<ChatService>();
        context.Services.AddScoped<OpenAiService>();

        return base.ConfigureServicesAsync(context);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    public override Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
    {
        IApplicationBuilder app = context.GetApplicationBuilder();
        app.UseEndpoints(endpoints =>
        {
            // open ai chat 对话
            endpoints.MapPost("/v1/chat/completions",
                    async (ChatService service, HttpContext context, AuroraAIChatCompletionsRequest request) =>
                        await service.ChatCompletionHandleAsync(context, request))
                .WithTags("OpenAI")
                .WithGroupName("OpenAI")
                .WithDescription("OpenAI Completions")
                .WithOpenApi();
            // open ai image 生成
            endpoints.MapPost("/v1/images/generations",
                    async (ChatService service, HttpContext context, AuroraAIImageCreateRequest request) =>
                        await service.CreateImageHandleAsync(context, request))
                .WithTags("OpenAI")
                .WithGroupName("OpenAI")
                .WithDescription("Image")
                .WithOpenApi();
            endpoints.MapPost("/v1/embeddings", async (ChatService embeddingService, HttpContext context) =>
                    await embeddingService.EmbeddingAsync(context))
                .WithDescription("OpenAI")
                .WithDescription("Embedding")
                .WithOpenApi();
            endpoints.MapPost("/v1/videos/generations",
                    async (ChatService videoService, HttpContext context, VideoCreateRequest request) =>
                        await videoService.CreateVideoTaskHandleAsync(context, request))
                .WithDescription("OpenAI")
                .WithDescription("Video")
                .WithOpenApi();
            endpoints.MapPost("/v1/videos/task/query",
                    async (ChatService videoService, HttpContext context, VideoTaskQueryRequest request) =>
                        await videoService.QueryVideoTaskHandleAsync(context, request))
                .WithDescription("OpenAI")
                .WithDescription("Video task query")
                .WithOpenApi();


            // chat/completions
            endpoints.MapPost("/api/open/v1/chat/completions",
                    async (OpenAiService service, HttpContext context) =>
                        await service.CompletionsAsync(context)) //OpenAiService.Completions
                .WithTags("OpenAI")
                .WithGroupName("OpenAI")
                .WithDescription("OpenAI Completions")
                .WithOpenApi();

            // chat/topic generate
            endpoints.MapPost("/api/open/v1/chat/topic/generate",
                    async (OpenAiService service, HttpContext context) =>
                        await service.GenerateTopicAsync(context)) //OpenAiService.Completions
                .WithTags("OpenAI")
                .WithGroupName("OpenAI")
                .WithDescription("OpenAI Chat topic generate")
                .WithOpenApi();


            // OpenAI images generations
            endpoints.MapPost("/api/open/v1/images/generations",
                    async (OpenAiService service, HttpContext context) => await service.ImageGenerate(context))
                .WithTags("OpenAI")
                .WithGroupName("OpenAI")
                .WithDescription("OpenAI images generations")
                .WithOpenApi();
            
            
            endpoints.MapPost("/api/open/v1/videos/generations",
                    async (OpenAiService service, HttpContext context) => await service.VideoGenerate(context))
                .WithTags("OpenAI")
                .WithGroupName("OpenAI")
                .WithDescription("OpenAI video generations")
                .WithOpenApi();
        });
        return base.OnApplicationInitializationAsync(context);
    }
}